package com.shanghaionstar.home.life;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shanghaionstar.R;

public class PayStatusDialog extends Dialog {
    private ViewHolder holder;
    private Context context;

    private IProtocolClickListener listener;

    public PayStatusDialog(Context context) {
        this(context, R.style.Theme_Dialog_NoTitleBar, null);
    }

    public PayStatusDialog(Context context, IProtocolClickListener listener) {
        this(context, R.style.Theme_Dialog_NoTitleBar, listener);
    }

    public PayStatusDialog(Context context, int theme, IProtocolClickListener listener) {
        super(context, theme);
        holder = new ViewHolder();
        this.context = context;
        View view = View.inflate(context, R.layout.pay_status_dia, null);
        holder.init(view);
        setContentView(view);
        setGravity();
        setWidthHeight();
        this.listener = listener;
        setCanceledOnTouchOutside(false);
    }


    /**
     * 居中
     */
    private void setGravity() {
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
    }

    /**
     * 横向全屏
     */
    private void setWidthHeight() {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth() * 4 / 5;
        int height = (int) (170 * context.getResources().getDisplayMetrics().density);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height);
        holder.protocolRl.setLayoutParams(lp);
    }


    public class ViewHolder {
        RelativeLayout protocolRl;
        ImageView closeIv;
        TextView refuseTv, agreeTv;

        public void init(View view) {
            protocolRl = (RelativeLayout) view.findViewById(R.id.smart_pro_rl);
            refuseTv = (TextView) view.findViewById(R.id.refuse_tv);
            agreeTv = (TextView) view.findViewById(R.id.agree_tv);
            closeIv = (ImageView) view.findViewById(R.id.close_iv);

            refuseTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(listener != null)
                        listener.onClick(refuseTv);
                }
            });

            agreeTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(listener != null)
                        listener.onClick(agreeTv);
                }
            });

            closeIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null)
                        listener.onClick(closeIv);
                }
            });
        }
    }

    public IProtocolClickListener getListener() {
        return listener;
    }

    public void setListener(IProtocolClickListener listener) {
        this.listener = listener;
    }

    public interface IProtocolClickListener{
        void onClick(View view);
    }
}
